package net.zjitc.controller;

import net.zjitc.pojo.User;
import net.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/3/26 11:11
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;
    @RequestMapping("/findAll")
    public String findAll(Model modle){
        List<User> list = userService.findAll();
        modle.addAttribute("list",list);
        return "/list.jsp";
    }

    @RequestMapping("/login")
    public String login(String username, String password, HttpSession session,Model model){
        User user = userService.login(username,password);
        if (user!= null){
            session.setAttribute("user",user);
            return "redirect:/index.do";
        }else {
            model.addAttribute("msg","用户名或密码错误");
            return "/client/login.jsp";
        }

    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "/client/index.jsp";
    }

}
